﻿using System.Collections.Generic;
using System.Web.Script.Serialization;
using Synology.DS.Interface.Entities.DiskStation;
using Synology.DS.Interface.Exception;

namespace Synology.DS.Core.DiskStation
{
    public class TaskService
    {
        private readonly StationAccessor _accessor;

        public TaskService(StationAccessor accessor)
        {
            _accessor = accessor;
        }

        public ServiceInfo QueryInfo()
        {
            object request = new
            {
                api = "SYNO.API.Info",
                method = "query",
                query = "SYNO.DownloadStation.Task",
                version = 1
            };

            Response response = _accessor.SubmitGetRequest("query.cgi", request);
            if (!response.success)
                throw new AccessDiskStationException(response.error.code);

            Dictionary<string, object> resultedDictionary = response.data;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.ConvertToType<ServiceInfo>(resultedDictionary["SYNO.DownloadStation.Task"]);
        }
      

        public void CreateTask(string sid, ServiceInfo info, string url)
        {
            object request = new
            {
                _sid = sid,
                api = "SYNO.DownloadStation.Task",
                method = "create",
                version = 1,
				uri = url
            };

            Response response = _accessor.SubmitPostRequest(info.path, request);
            if (!response.success)
                throw new AccessDiskStationException(response.error.code);
        }
    }
}
